{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter\n",
    "import tkinter.filedialog\n",
    "import tkinter.messagebox\n",
    "import os\n",
    "from PIL import ImageGrab,Image\n",
    "from time import sleep\n",
    "\n",
    "#创建tkinter应用程序，设置窗口大小和位置，不允许窗口改变大小\n",
    "root = tkinter.Tk()\n",
    "root.geometry('100x40+400+300')\n",
    "root.resizable(False,False) \n",
    "\n",
    "\n",
    "class MyCapture:\n",
    "   def __init__(self,png):\n",
    "      #变量X和Y用来记录鼠标左键按下的位置\n",
    "      self.X=tkinter.IntVar(value=0)\n",
    "      self.Y=tkinter.IntVar(value=0)\n",
    "\n",
    "      #屏幕尺寸\n",
    "      screenWidth=root.winfo_screenwidth()\n",
    "      screenHeight=root.winfo_screenheight()\n",
    "\n",
    "      #创建顶级组件容器\n",
    "      self.top=tkinter.Toplevel(root,width=screenWidth,height=screenHeight)\n",
    "      #不显示最大化、最小化按钮\n",
    "      self.top.overrideredirect(True)\n",
    "      self.canvas=tkinter.Canvas(self.top,bg='white',width=screenWidth,height=screenHeight)\n",
    "      #显示全屏截图，在全屏截图上进行区域截图\n",
    "      self.image=tkinter.PhotoImage(file=png)\n",
    "      self.canvas.create_image(screenWidth//2,screenHeight//2,image=self.image)\n",
    "\n",
    "      #获取鼠标左键抬起的位置，取色，并转换成合适的颜色格式进行显示\n",
    "      def onLeftButtonUp(event):\n",
    "        im=Image.open(png)\n",
    "        color=im.getpixel((event.x,event.y))[:3]\n",
    "        msg=str(color)+'\\n'\n",
    "        color='#'+''.join(map(lambda x: hex(x)[2:], color))\n",
    "        msg+=color\n",
    "        tkinter.messagebox.showinfo('',msg)\n",
    "\n",
    "        #关闭当前窗口\n",
    "        self.top.destroy()\n",
    "      self.canvas.bind('<ButtonRelease-1>',onLeftButtonUp)\n",
    "\n",
    "      self.canvas.pack(fill=tkinter.BOTH,expand=tkinter.YES)\n",
    "#开始截图\n",
    "def buttonCaptureClick():\n",
    "    #最小化主窗口\n",
    "    root.state('icon')\n",
    "    sleep(0.2)\n",
    "\n",
    "    filename='temp.png'\n",
    "    im = ImageGrab.grab()\n",
    "    im.save(filename)\n",
    "    im.close()\n",
    "    \n",
    "    #显示全屏幕截图\n",
    "    w=MyCapture(filename)\n",
    "    buttonCapture.wait_window(w.top)\n",
    "\n",
    "    #截图结束，恢复主窗口，并删除临时的全屏幕截图文件\n",
    "    root.state('normal')\n",
    "    os.remove(filename)\n",
    "buttonCapture = tkinter.Button(root,text='取色',command=buttonCaptureClick)\n",
    "buttonCapture.place(x=10,y=10,width=80,height=20)\n",
    "\n",
    "#启动消息主循环\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
